﻿using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TShockAPI;
using TShockAPI.DB;

namespace BuffPrefixV2
{
    public static class DBManager
    {
        public static void CreatePlayersTable() {
            SqlTable table = new SqlTable("prefixplayers", new SqlColumn[] {
            new SqlColumn("Name",MySqlDbType.VarChar,255){ Unique=true},
            new SqlColumn("CurrentPrefixID",MySqlDbType.Int32,255),
            new SqlColumn("PrefixPossessions",MySqlDbType.VarChar,255)
            });
            var sqlWriter = new SqlTableCreator(TShock.DB, TShock.DB.GetSqlType() == SqlType.Sqlite ? (IQueryBuilder)new SqliteQueryCreator() : new MysqlQueryCreator());
            sqlWriter.EnsureTableStructure(table);
        }
        public static void CreatePrefixesTable()
        {
            SqlTable table = new SqlTable("prefixes", new SqlColumn[] {
            new SqlColumn("ID",MySqlDbType.Int32){Primary=true,AutoIncrement=true},
            new SqlColumn("Name",MySqlDbType.VarChar,255){ Unique=true},
            new SqlColumn("Buffs",MySqlDbType.VarChar,255)
            });
            var sqlWriter = new SqlTableCreator(TShock.DB, TShock.DB.GetSqlType() == SqlType.Sqlite ? (IQueryBuilder)new SqliteQueryCreator() : new MysqlQueryCreator());
            sqlWriter.EnsureTableStructure(table);
        }
        #region 称号表管理
        public static void AddPrefix(Prefix prefix) {
            string query = $"INSERT INTO prefixes (ID,Name,NameColor,PrefixColor,TextColor,Buffs) VALUES ({prefix.ID},'{prefix.PrefixName}','{prefix.IntoString()}')";
            TShock.DB.Query(query);
        }
        public static void UpdatePrefix(Prefix prefix) {
            string query = $"UPDATE prefixes SET ID={prefix.ID},Name='{prefix.PrefixName}',Buffs='{prefix.IntoString()}' WHERE ID={prefix.ID}";
            TShock.DB.Query(query);
        }
        public static void DeletePrefix(int prefixID)
        {
            string query = $"DELETE FROM prefixes WHERE ID={prefixID} ";
            TShock.DB.Query(query);
        }
        public static  Prefix GetPrefix(int prefixID) {
            string query = $"SELECT * FROM prefixes WHERE ID={prefixID};";
            using (var reader = TShock.DB.QueryReader(query))
            {
                if (reader.Read())
                {
                    int id = reader.Get<int>("ID");
                    string  name = reader.Get<string>("Name");
                    string buffString = reader.Get<string>("Buffs");
                    var buffArray = buffString.Split(',');
                    List<int> buffList = new List<int>();
                    if ((buffArray.Length != 0) && (!string.IsNullOrEmpty(buffString)))
                    {
                        for (int i = 0; i < buffArray.Length; i++)
                        {
                            buffList.Add(int.Parse(buffArray[i]));
                        }
                        
                    }
                    return new Prefix(id, name, buffList);
                }
                else { return null; }
            }
        }
        public static List<Prefix> GetAllPrefixes()
        {
            string query = $"SELECT * FROM prefixes;";
            using (var reader = TShock.DB.QueryReader(query))
            {
                List<Prefix> prefixes = new List<Prefix>();
                while (reader.Read())
                {
                    int id = reader.Get<int>("ID");
                    string name = reader.Get<string>("Name");
                    string buffString = reader.Get<string>("Buffs");
                    var buffArray = buffString.Split(',');
                    List<int> buffList = new List<int>();
                    if ((buffArray.Length!=0)&&(!string.IsNullOrEmpty(buffString)))
                    {
                        for (int i = 0; i < buffArray.Length; i++)
                        {
                            buffList.Add(int.Parse(buffArray[i]));
                        }
                        
                    }
                    prefixes.Add(new Prefix(id, name, buffList));
                }
                return prefixes;
            }

        }
        #endregion
        #region 玩家表管理
        public static void AddPrefixPlayer(PrefixPlayer plr)
        {
            string query = $"INSERT INTO prefixplayers (Name,CurrentPrefixID,PrefixPossessions) VALUES ('{plr.Name}',{plr.CurrentPrefixID},'{plr.IntoString()}')";
            TShock.DB.Query(query);
        }
        public static void UpdatePrefixPlayer(PrefixPlayer plr)
        {
            string query = $"UPDATE prefixplayers SET Name='{plr.Name}',CurrentPrefixID={plr.CurrentPrefixID},PrefixPossessions='{plr.IntoString()}' WHERE Name='{plr.Name}'";
            TShock.DB.Query(query);
        }
        public static void DeletePrefixPlayer(string name)
        {
            string query = $"DELETE FROM prefixplayers WHERE Name='{name}'";
            TShock.DB.Query(query);
        }
        public static PrefixPlayer GetPrefixPlayer(string name)
        {
            string query = $"SELECT * FROM prefixplayers WHERE Name='{name}';";
            using (var reader = TShock.DB.QueryReader(query))
            {
                if (reader.Read())
                {
                    string plrName = reader.Get<string>("Name");
                    int currentPrefixID = reader.Get<int>("CurrentPrefixID");
                    string possessionsString = reader.Get<string>("PrefixPossessions");
                    var possessionsArray = possessionsString.Split(',');
                    List<int> possessionsList = new List<int>();
                    if ((possessionsArray.Length != 0) && (string.IsNullOrEmpty(possessionsString) != true))
                    {
                        for (int i = 0; i < possessionsArray.Length; i++)
                        {
                            possessionsList.Add(int.Parse(possessionsArray[i]));
                        }
                    }
                    return new PrefixPlayer( name,currentPrefixID,possessionsList);
                }
                else { return null; }
            }
        }
        public static List<PrefixPlayer> GetAllPrefixPlayers()
        {
            string query = $"SELECT * FROM prefixplayers;";
            using (var reader = TShock.DB.QueryReader(query))
            {
                List<PrefixPlayer> prefixePlayers = new List<PrefixPlayer>();
                while (reader.Read())
                {
                    string plrName = reader.Get<string>("Name");
                    int currentPrefixID = reader.Get<int>("CurrentPrefixID");
                    string possessionsString = reader.Get<string>("PrefixPossessions");
                    var possessionsArray = possessionsString.Split(',');
                    List<int> possessionsList = new List<int>();
                    if ((possessionsArray.Length!=0)&&(string.IsNullOrEmpty(possessionsString)!=true))
                    {
                        for (int i = 0; i < possessionsArray.Length; i++)
                        {
                            possessionsList.Add(int.Parse(possessionsArray[i]));
                        }
                    }
                    prefixePlayers.Add(new PrefixPlayer(plrName,currentPrefixID,possessionsList));
                }
                return prefixePlayers;
            }

        }



        #endregion
    }
}
